<?php
/**
 * PK
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class PkController extends BaseController {

	public function index(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$psize = I('get.psize','','intval');
		$p = I('get.p','','intval');
		$sector_id = I('get.sector_id',false,'intval');
		$key = I('get.key',false,'intval');
		$type = I('get.type',false,'intval');
		$start_time = I('get.start_time',' ','trim');
		$end_time = I('get.end_time','','trim');
		$goal_res = I('get.result',false,'intval');

		$wheres = array();
		$wheres['_string'] = '1=1';
		$MSector = M('\Admin\Model\GoalsModel:Sector');
		$arr = $MSector->getChild($sector_id);
		//获取权限个人
		$whereUser = array();
		$whereUser['sector_id'] = array('IN',$arr);
		$arrUser = M('User')->where($whereUser)->select();
		$dlsit_user = array();
		$lengthUser = sizeof($arrUser);
		for($j=0;$j<$lengthUser;$j++){
			$dlsit_user[$j] = $arrUser[$j]['user_id'];
		}

		if($key==1){
			$wheres['a.start_time'] = array('LT',date('Y-m-d H:i:s'));
			$wheres['a.end_time'] = array('GT',date('Y-m-d H:i:s'));
		}else if($key==2){
			$wheres['a.end_time']  = array('LT',date('Y-m-d H:i:s'));
		}else if($key==3){
			$wheres['a.start_time'] = array('GT',date('Y-m-d H:i:s'));
		}
		if($start_time){
			$wheres['a.start_time'] = array('LIKE','%'.$start_time.'%');
		}
		if($end_time){
			$wheres['a.end_time'] = array('LIKE','%'.$end_time.'%');
		}
		if($goal_res){
			$wheres['a.pk_res'] = $goal_res-1;
		}
		if($type==1){
			$wheres1 = array();			//正反方范围
			$wheres['a.a_type'] = $type;
			$wheres['a.b_type'] = $type;
			$wheres1['c.sector_id'] = array('IN',$arr);
			$wheres1['d.sector_id'] = array('IN',$arr);
			$wheres1['_logic'] = 'OR';

			$where11 = array($wheres,$wheres1,'_logic'=>'AND');
			$cnt = M()	->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.user_id,c.sector_id,c.nickname as a_object,d.user_id,d.sector_id,d.nickname as b_object")  
						->table('sales_pk as a')
						->join('user as c on c.user_id = a.a')
						->join('user as d on d.user_id = a.b')
						->where($where11)
						->group('a.pkid')
						->order('a.create_time desc')
						->select();
			$cnt = sizeof($cnt);
			$Page = new \Common\ORG\Page( $cnt, $psize );
			
			$dlist = M()->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.user_id,c.sector_id,c.nickname as a_object,d.user_id,d.sector_id,d.nickname as b_object")
						->table('sales_pk as a')
						->join('user as c on c.user_id = a.a')
						->join('user as d on d.user_id = a.b')
						->where($where11)
						->group('a.pkid')
						->order('a.create_time desc')
					  	->limit($Page->firstRow.','.$Page->listRows )
						->select();
			if($dlist){
				$res['rcode'] = 1;
				$res['data'] = $dlist;
				$res['msg'] = $arr1;
				$res['pall'] = $Page->getTotalPages();
				$res['p'] = $Page->nowPage;
				$res['psize'] = $psize;
			}
		}
		else if($type==2){
			$wheres2 = array();		//正反方范围
			$wheres['a.a_type'] = $type;
			$wheres['a.b_type'] = $type;
			$wheres2['a.a'] = array('IN',$arr);
			$wheres2['a.b'] = array('IN',$arr);
			$wheres2['_logic'] = 'OR';
			$where  = array($wheres,$wheres2,'_logic'=>'AND');
			$cnt = M()->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.sector_id,c.info as a_object,d.sector_id,d.info as b_object")
						->table('sales_pk as a')
						->join('sector as c on c.sector_id = a.a')
						->join('sector as d on d.sector_id = a.b')
						->where($where)
						->group('a.pkid')
						->order('a.create_time desc')
						->select();
			$cnt = sizeof($cnt);
			$Page = new \Common\ORG\Page( $cnt, $psize );
			$dlist = M()->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.sector_id,c.info as a_object,d.sector_id,d.info as b_object")
						->table('sales_pk as a')
						->join('sector as c on c.sector_id = a.a')
						->join('sector as d on d.sector_id = a.b')
						->where($where)
						->group('a.pkid')
						->order('a.create_time desc')
					  	->limit($Page->firstRow.','.$Page->listRows )
						->select();

			if($dlist){
				$res['rcode'] = 1;
				$res['data'] =$dlist;
				$res['msg'] = $cnt;
				$res['pall'] =  $Page->getTotalPages();
				$res['p'] = $Page->nowPage;
				$res['psize'] = $psize;
			}

		}else if($type==3){
			$wheres31 = array();		//人、部门
			$where31 = array();			//第一种情况归属范围
			$wheres31['a.a_type'] = 1;
			$wheres31['a.b_type'] = 2;
			$wheres31['_logic']	= 'AND';
			$where31['c.user_id'] = array('IN',$dlsit_user);
			$where31['d.sector_id'] = array('IN',$arr);
			$where31['_logic'] = 'OR';

			$wheres32 = array();		//部门、人
			$where32 = array();			//第二种情况归属范围
			$wheres32['a.a_type'] = 2;
			$wheres32['a.b_type'] = 1;
			$wheres32['_logic'] = 'AND';
			$where32['c.sector_id'] = array('IN',$arr);
			$where32['d.user_id'] = array('IN',$dlsit_user);
			$where32['_logic'] = 'OR';

			$where331 = array($wheres,$wheres31,$where31,'_logic'=>'AND');
			$dlist31 = M()->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.nickname as a_object,d.info as b_object")
						->table('sales_pk as a')
						->join('user as c on c.user_id = a.a')
						->join('sector as d on d.sector_id = a.b')
						->where($where331)
						->group('a.pkid')
						->order('a.create_time desc')
						->select();

			$where332 = array($wheres,$wheres32,$where32,'_logic'=>'AND');
			$dlist32 = M()->field("a.a_mount,a.b_mount,a.pk_type,a.pkid,a.a,a.a_type,a.a_goal,a.b,a.b_type,a.b_goal,a.start_time,a.end_time,a.b_prize,a.a_prize,a.pk_res,a.last_time,a.info,c.info as a_object,d.nickname as b_object")
						->table('sales_pk as a')
						->join('sector as c on c.sector_id = a.a')
						->join('user as d on d.user_id = a.b')
						->where($where332)
						->group('a.pkid')
						->order('a.create_time desc')
						->select();
			
			$dlist33 = $dlist31;
			$length_dlist31 = sizeof($dlist31);
			$length_dlist32 = sizeof($dlist32);
			for($i=0;$i<$length_dlist32;$i++){
				$dlist33[$i+$length_dlist31] = $dlist32[$i];
			}
			$cnt = sizeof($dlist33);
			$Page = new \Common\ORG\Page( $cnt, $psize );
			$Dlist = array();
			for($i=($p-1)*$psize;$i<$p*$psize;$i++){
				if($dlist33[$i]){
					$Dlist[$i] = $dlist33[$i];
				}
			}
			if($dlist33){
				$res['rcode'] = 1;
				$res['data'] = $dlist33;
				$res['msg'] = $dlist31;
				$res['pall'] = $Page->getTotalPages();
				$res['p'] = $Page->nowPage;
				$res['psize'] = $psize;
			}
		}
		$res['time'] = date('Y-m-d H:i:s');
		e_json($res);
	}
	/*增加PK*/
	public function addPK(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$start_time = I('post.start_time',false,'trim');
		$end_time = I('post.end_time',false,'trim');
		$info = I('post.info',false,'trim');
		$a_type = I('post.a_type',false,'intval');
		$b_type = I('post.b_type',false,'intval');
		$a = I('post.aname','','intval');
		$b = I('post.bname','','intval');
		$a_goal = I('post.a_goal',false,'intval');
		$b_goal = I('post.b_goal',false,'intval');
		$b_prize = I('post.b_prize',false,'intval');
		$a_prize = I('post.a_prize',false,'intval');
		$pk_type = I('post.pk_type',false,'intval');

		$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
		$uinfo =$MTags->index();
		$d = array();
		$d['a'] = $a;
		$d['b'] = $b;
		$d['a_type'] = $a_type;
		$d['b_type'] = $b_type;
		$d['start_time'] = $start_time;
		$d['end_time'] = $end_time;
		$MPk = M('pk','sales_');
		$list1 = $MPk->where($d)->select();
		$whered['a'] = $b;
		$whered['b'] = $a;
		$whered['start_time'] = $start_time;
		$whered['end_time'] = $end_time;
		$list2 = $MPk->where($whered)->select();
		if($list1 || $list2){
			$res['rcode'] = 2;
		}else{
			$d['info'] = $info;
			$d['last_time'] = date('Y-m-d H:i:s');
			$d['create_time'] = date('Y-m-d H:i:s');
			$d['a_sector'] = $this->getSector( $d['a'], $d['a_type']);
			$d['a_goal'] = $a_goal;
			$d['a_sector'] = $this->getSector( $d['a'], $dataExcel['d']);
			$d['b_goal'] = $b_goal;
			$d['b_prize'] = $b_prize;
			$d['a_prize'] = $a_prize;
			$d['pk_type'] = $pk_type;
			$d['creator'] = $uinfo['user_id'];
			$d['creator_sector'] = $uinfo['sector_id'];
			$d['creator_role'] = $uinfo['role_id'];

			if($a_type==1){
				$d['catcher_type'] = 1;
				if($object){
					$MUser = M('User');
					$where1 = array();
					$where1['nickname'] = array('eq',$object);
					$arr1 = $MUser->field('user_id,nickname')->where($where1)->select();
					if($arr1){
						$d['a'] = $arr1[0]['user_id'];
					}
				}
			}else if($a_type==2){
				$d['catcher_type'] = 2;
				if($object){
					$MSector = M('Sector');
					$where2 = array();
					$where2['info'] = array('eq',$object);
					$arr2 = $MSector->field('sector_id,info')->where($where2)->select();
					if($arr2){
						$d['a'] = $arr2[0]['sector_id'];
					}
				}
			}
			if($b_type==1){
				$d['catcher_type'] = 1;
				if($object){
					$MUser = M('User');
					$where1 = array();
					$where1['nickname'] = array('eq',$object);
					$arr1 = $MUser->field('user_id,nickname')->where($where1)->select();
					if($arr1){
						$d['b'] = $arr1[0]['user_id'];
					}
				}
			}else if($b_type==2){
				$d['catcher_type'] = 2;
				if($object){
					$MSector = M('Sector');
					$where2 = array();
					$where2['info'] = array('eq',$object);
					$arr2 = $MSector->field('sector_id,info')->where($where2)->select();
					if($arr2){
						$d['b'] = $arr2[0]['sector_id'];
					}
				}
			}
			$MPk = M('pk','sales_');
			$addID = $MPk->add($d);
			if($addID){
				$wheres = array();
				$wheres['goal_id'] = $addID;
				$find = $MPk->where($wheres)->find();
				if($find){
					$res['rcode'] = 1;
					$res['data'] = $find;	
				}
			}
		}
		e_json($res);
	}
	/*修改PK*/
	public function editPK(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$pkid = I('post.pkid',false,'intval');	
		$a_goal = I('post.edit_a_goal',false,'intval');
		$b_goal = I('post.edit_b_goal',false,'intval');
		$b_prize = I('post.edit_b_prize',false,'intval');
		$a_prize = I('post.edit_a_prize',false,'intval');
		$info = I('post.edit_info',false,'trim');

		$d = array();
		$d['a_goal'] = $a_goal;
		$d['b_goal'] = $b_goal;
		$d['b_prize'] = $b_prize;
		$d['a_prize'] = $a_prize;		
		$d['info'] = $info;

		$wheres = array();
		$wheres['pkid'] = $pkid;
		$MPk = M('pk','sales_');
		$MPk->where($wheres)->save($d);
		if(1){
			$res['rcode'] = 1;
			$res['data'] = $MPk->where($wheres)->select();
 		}
		e_json($res);
	}
	/* 删除PK*/
	public function delShowPk(){
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$pkid = I('get.pkid',false,'intval');

		$wheres = array();
		$wheres['pkid'] = $pkid;
		$MPk = M('pk','sales_');
		if($MPk->where($wheres)->delete()){
			$res['rcode'] = 1;
		}
		e_json($res);
	}
	/*PK导出*/
    public function pkExport(){	
		Vendor('PHPExcel.PHPExcel');
		Vendor("PHPExcel.PHPExcel.IOFactory");
		$objPHPExcel = new \PHPExcel();
		// Set properties  
		$objPHPExcel->getProperties()->setCreator("slackck")//创建人
		->setLastModifiedBy("slackck")//最后修改人
		->setTitle("数据EXCEL导出")//标题
		->setSubject("数据EXCEL导出")//题目
		->setDescription("和众汇富")//描述
		->setKeywords("excel")//关键字
		->setCategory("result file");//种类
		
		// set table header content  
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1', '部门编号')
		->setCellValue('B1', '部门')
		->setCellValue('C1', 'A方姓名')
		->setCellValue('D1', '业绩/个数')
		->setCellValue('E1', '赌注')
		->setCellValue('F1', 'B方姓名')
		->setCellValue('G1', '业绩/个数')
		->setCellValue('H1', '赌注')
		->setCellValue('I1', '最终获胜者')	
		->setCellValue('J1', '最终失败者')
		->setCellValue('K1', 'PK金额');

		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		$start_time = I('get.start_time','','trim');
		$end_time = I('get.end_time','','trim');
		$goal_res = I('get.pk_res','','trim');

		$wheres = array();
		if($start_time){
			$wheres['a.start_time'] = array('LIKE','%'.$start_time.'%');
		}
		if($end_time){
			$wheres['a.end_time'] = array('LIKE','%'.$end_time.'%');
		}
		if($goal_res){
			$wheres['a.pk_res'] = $goal_res-1;
		}

		$res = M() ->field("b.nickname as aname,c.sector_name as aaname,d.nickname as bname,e.sector_name as bbname,a.a_prize as prizea,a.a_mount as mounta, a.b_mount as mountb, a.b_prize as prizeb,f.sector_name as sector_name,
				CASE  WHEN a.a_type = 1  THEN b.nickname
						WHEN a.a_type = 2  THEN c.sector_name
						ELSE 'A方PK人员不存在' END AS name_a,
				CASE  WHEN a.b_type = 1 THEN d.nickname
						WHEN a.b_type = 2  THEN e.sector_name
						ELSE 'B方PK人员不存在' END AS name_b,
				CASE  WHEN a.pk_res = 1 && a.a_type = 1 THEN b.nickname
						WHEN a.pk_res = 1 && a.a_type = 2 THEN c.sector_name
						WHEN a.pk_res = 2 && a.b_type = 1 THEN d.nickname
						WHEN a.pk_res = 2 && a.b_type = 2 THEN e.sector_name
						WHEN a.pk_res = 0 THEN '未处理'
						ELSE '平手' END AS win,
				CASE  WHEN a.pk_res = 1 && a.b_type = 1 THEN d.nickname
						WHEN a.pk_res = 1 && a.b_type = 2 THEN e.sector_name
						WHEN a.pk_res = 2 && a.a_type = 1 THEN b.nickname
						WHEN a.pk_res = 2 && a.a_type = 2 THEN c.sector_name
						WHEN a.pk_res = 0 THEN '未处理'
						ELSE '平手' END AS fal,
				CASE  WHEN a.pk_res = 1 THEN a.b_prize
						WHEN a.pk_res = 2 THEN a.a_prize
						ELSE '0' END AS pkprize"
						)
					->table('sales_pk as a')
					->join('user as b on b.user_id = a.a','LEFT')
					->join('sector as c on c.sector_id = a.a','LEFT')
					->join('user as d on d.user_id = a.b','LEFT')
					->join('sector as e on e.sector_id = a.b','LEFT')
					->join('sector as f on f.sector_id = a.a_sector')
					->where($wheres)
					->order('a.a_sector asc')
					->select();

		// Miscellaneous glyphs, UTF-8
		for($i=0;$i<=count($res)-1;$i++){
			$objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+2), $i);
			$objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+2), $res[$i]['sector_name']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+2), $res[$i]['name_a']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+2), $res[$i]['mounta']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+2), $res[$i]['prizea']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+2), $res[$i]['name_b']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('G'.($i+2), $res[$i]['mountb']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('H'.($i+2), $res[$i]['prizeb']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('I'.($i+2), $res[$i]['win']);		
			$objPHPExcel->getActiveSheet(0)->setCellValue('J'.($i+2), $res[$i]['fal']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('K'.($i+2), $res[$i]['pkprize']);
		}
	
		//sheet命名  
		$objPHPExcel->getActiveSheet()->setTitle('PK结果汇总');
	
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet  
		$objPHPExcel->setActiveSheetIndex(0);  
	
		//excel头参数  
		//header("Content-Type: application/vnd.ms-excel; charset=gb2312"); 
		//header('Content-Disposition: attachment;filename="'.$random.'.xls"');  //日期为文件名后缀  
		//header('Cache-Control: max-age=0');  
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls格式，excel2007为xlsx格式 
		//$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); 	
		ob_end_clean(); // Added by me
		ob_start(); // Added by me        
		$objWriter->save('php://output'); 
		// $objWriter->save('D:/'.$random.''.'.xls'); //这块修改路径。路径写到哪里，就会保存到什么地方。

	}
	/*PK导入*/
	public function pkImport() {
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');

		$upload = new \Vendor\Upload\Upload();
    	$upload->maxSize   =     3145728;
    	$upload->exts      =     array('xls', 'csv', 'xlsx');  
    	$upload->rootPath  =     './../www/uploads';
    	$upload->savePath  =      '/excel/';  
    	$info   =   $upload->upload();
    	if(!$info){
    		$this->error($upload->getError());
    	}else{
			$filename='./../www/uploads'.$info['file']['savepath'].$info['file']['savename'];
			
    		import("Vendor.PHPExcel.ExcelReader");
    		$ExcelReader=new \ExcelReader();
    		$arr=$ExcelReader->reader_excel($filename);

			/*未能插入成功的数据*/
			$addError = array();
			$start_time = date('Y-m-01', strtotime(date("Y-m-d")));
			$MTags = M('\Admin\Model\TagsModel:Tag','customer_');
			$uinfo =$MTags->index();
    		foreach ($arr as $key => $value) {
				$dataExcel = array();
				$dataExcel['name_a'] = $arr[$key]['1'];
				$dataExcel['name_b'] = $arr[$key]['5'];
				$dataExcel['a_type'] = $this->getType( $arr[$key]['2']);
				$dataExcel['b_type'] = $this->getType( $arr[$key]['6']);
				$dataExcel['a'] = $this->getName( $dataExcel['name_a'], $dataExcel['a_type']);
				$dataExcel['b'] = $this->getName( $dataExcel['name_b'], $dataExcel['b_type']);
				$dataExcel['start_time'] = $start_time;
				$dataExcel['end_time'] = date("Y-m-d", strtotime("$start_time + 1 month - 1 day"));

				/*判断是否存在PK冲突现象，同一时间段、相同A、B方的PK*/	
				$MPk = M('pk','sales_');
				$d1 = array();
				$d1['a'] = $dataExcel['a'];
				$d1['b'] = $dataExcel['b'];
				$d1['a_type'] = $dataExcel['a_type'];
				$d1['b_type'] = $dataExcel['b_type'];
				$d1['start_time'] = $dataExcel['start_time'];
				$d1['end_time'] = $dataExcel['end_time'];
				$list1 = $MPk->where($d1)->find();
				$d2 = array();
				$d2['a'] = $dataExcel['b'];
				$d2['b'] = $dataExcel['a'];
				$d2['a_type'] = $dataExcel['a_type'];
				$d2['b_type'] = $dataExcel['b_type'];
				$d2['start_time'] = $dataExcel['start_time'];
				$d2['end_time'] = $dataExcel['end_time'];
				$list2 = $MPk->where($d2)->find();

				if( !$list1 && !$list2) {
					$dataExcel['a_goal'] = $arr[$key]['3'];
					$dataExcel['a_prize'] = $arr[$key]['4'];
					$dataExcel['b_goal'] = $arr[$key]['7'];
					$dataExcel['b_prize'] = $arr[$key]['8'];
					$dataExcel['info'] = 'PK批量导入';
					$dataExcel['creator'] = $uinfo['user_id'];
					$dataExcel['creator_sector'] = $uinfo['sector_id'];
					$dataExcel['creator_role'] = $uinfo['role_id'];
					$dataExcel['create_time'] = date('Y-m-d H:i:s');
					$dataExcel['last_time'] = date('Y-m-d H:i:s');

					$dataExcel['a_sector'] = $this->getSector( $dataExcel['a'], $dataExcel['a_type']);
					$dataExcel['b_sector'] = $this->getSector( $dataExcel['b'], $dataExcel['b_type']);

					$addID = M('Pk','sales_')->add($dataExcel);
					if( !$addID) {
						$addError[$key] = floatval( $arr[$key]['0'] );
						$res['msg'] = '导入出错';
					}
				}else {
					$addError[$key] = floatval( $arr[$key]['0'] );
				}
    		}
			$res['data'] = $addError;
			if( !$res['data']) {
				$res['rcode'] = 1;
			}
    	}
		e_json($res);
	}
	/*获取对象类型*/
	protected function getType($type){
		$ty = '';
		if( $type == '个人'){
			$ty = 1;
		}else if( $type == '部门'){
			$ty = 2;
		}
		return $ty;
	}
	/*获取对象id*/
	protected function getName($name, $type){
		$na = [];
		if( $type == '1') {
			$na = M('User')->field('user_id as id')->where("nickname = '%s'",$name)->find();
		}else if( $type == '2') {
			$na = M('Sector')->field('sector_id as id')->where("sector_name = '%s'",$name)->find();
		}
		return $na['id'];
	}
	/*获取对象顶级部门*/
	protected function getSector($id, $type){
		$parent_id = '';
		if( $type == 1) {
			$dlist1 = M('user')->field('sector_id')->where("user_id = '%d'",$id)->find();
			$sector_id = $dlist1['sector_id'];
			$dlist2 = M('sector')->field('parent_sector')->where("sector_id = '%d'",$sector_id)->find();
			$parent_id = $dlist2['parent_sector'];
		}else if( $type == 2) {
			$dlist1 = M('sector')->field('parent_sector')->where("sector_id = '%d'",$id)->find();
			$parent_id = $dlist1['parent_sector'];
		}
		return $parent_id;
	}


		
}